$(function() {
	 var count = 60; //间隔函数，1秒执行
	 var InterValObj; //timer变量，控制时间
	 var curCount;//当前剩余秒数
	 var mail  = /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
	 var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;//手机号正则
	 var allCode ;
	 var InterValObj2;
	$("#mail").click(function() {
		var email = $("#email").val();
		if (mail.test(email)) {
			sendMessage1($(this));
			showMsg('发送成功！','top','successful');
			console.log(email)
			sendCode("mailBox",email);
		}else {
			showMsg('邮箱格式不正确！','top','error');
		}
	})
	
	$("#register1").click(function() {
		var code = $("#code1").val();
		var pwd = $("#pwd1").val();
		var newPwd = $("#newpwd1").val();
		var email = $("#email").val();
		if(email === ""){
			showMsg('邮箱不能为空！','top','error');
		}else if( pwd === ""){
			showMsg('密码不能为空！','top','error');
		}else if(pwd != newPwd){
			showMsg('两次密码输入不一致！','top','error');
		}else if(code != allCode){
			showMsg('验证码输入错误！','top','error');
		}else{
			$.ajax({
		        type: "POST",
		        url: "/register/email",
		        data: {
		        	"email":email,
		        	"password":newPwd
		        },
		        dataType:"json",
		        success: function(result){
		        	if (result) {
		        		alert('注册成功！');
		        		window.location.href = "/login";
		        		showMsg('注册成功！','top','successful');
					}else{
						showMsg('注册失败,当前用户已经存在！','top','error');
					}
		        },
		        error:function (xml) {
		          console.log(0);
		        }
			});
		}
	})
	
	$("#picture").click(function() {
		var randomNum = ('000000' + Math.floor(Math.random() * 999999)).slice(-4);
		console.log(randomNum)
		$(this).val(randomNum);
	})
	
	$("#register2").click(function() {
		var code = $("#code2").val();
		var pwd = $("#pwd2").val();
		var newPwd = $("#newpwd2").val();
		var name = $("#name").val();
		var check = $("#picture").val();
		if(name === ""){
			showMsg('用户名不能为空！','top','error');
		}else if( pwd === ""){
			showMsg('密码不能为空！','top','error');
		}else if(pwd != newPwd){
			showMsg('两次密码输入不一致！','top','error');
		}else if(code != check){
			showMsg('验证码输入错误！','top','error');
		}else {
			$.ajax({
		        type: "POST",
		        url: "/register",
		        data: {
		        	"name":name,
		        	"password":newPwd
		        },
		        dataType:"json",
		        success: function(result){
		        	if (result) {
		        		alert('注册成功！');
		        		window.location.href = "/login";
		        		showMsg('注册成功！','top','successful');
					}else{
						showMsg('注册失败,当前用户已经存在！','top','error');
					}
		        },
		        error:function (xml) {
		          console.log(0);
		        }
			});
		}
	})
	
	$("#phoneCode").click(function() {
		var phone = $("#phone").val();
		if(phoneReg.test(phone)){
			sendMessage2($(this));
			sendCode("Sms",phone);
			showMsg('发送成功！','top','successful');
		}else{
			showMsg('请输入正确的手机格式！','top','error');
		}
	})
	
	$("#register3").click(function() {
		var code = $("#code3").val();
		var pwd = $("#pwd3").val();
		var newPwd = $("#newpwd3").val();
		var phone = $("#phone").val();
		if(phone === ""){
			showMsg('手机号码不能为空！','top','error');
		}else if( pwd === ""){
			showMsg('密码不能为空！','top','error');
		}else if(pwd != newPwd){
			showMsg('两次密码输入不一致！','top','error');
		}else if(code != allCode){
			showMsg('验证码输入错误！','top','error');
		}else{
			$.ajax({
		        type: "POST",
		        url: "/register/Sms",
		        data: {
		        	"phone":phone,
		        	"password":newPwd
		        },
		        dataType:"json",
		        success: function(result){
		        	if (result) {
		        		alert('注册成功！');
		        		window.location.href = "/login";
		        		showMsg('注册成功！','top','successful');
              }else{
                showMsg('注册失败,当前用户已经存在！','top','error');
              }
		        },
		        error:function (xml) {
		          console.log(0);
		        }
			});
		}
	})
	
	function sendMessage1(selector) {
	    curCount = count;
	    //设置button效果，开始计时
	    selector.attr("disabled", "true");
	    selector.val( + curCount + "秒");
	    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
	    //向后台发送处理数据

  }
  function SetRemainTime() {
    if (curCount == 0) {
      window.clearInterval(InterValObj);//停止计时器
      $("#mail").removeAttr("disabled");//启用按钮
      $("#mail").val("重新发送");
    }
    else {
    	curCount--;
    	$("#mail").val( + curCount + "秒");
    }
  }
  function sendMessage2(selector) {
	    curCount = count;
	    //设置button效果，开始计时
	    selector.attr("disabled", "true");
	    selector.val( + curCount + "秒");
	    InterValObj1 = window.setInterval(SetRemainTime2, 1000); //启动计时器，1秒执行一次
	    //向后台发送处理数据

	}
	function SetRemainTime2() {
	  if (curCount == 0) {
	    window.clearInterval(InterValObj1);//停止计时器
	    $("#phoneCode").removeAttr("disabled");//启用按钮
	    $("#phoneCode").val("重新发送");
	  }
	  else {
	  	curCount--;
	  	$("#phoneCode").val( + curCount + "秒");
	  }
	}
  
  function sendCode(operate,data) {
	  $.ajax({
	        type: "POST",
	        url: "/register/sendCode",
	        data: {
	        	"operate":operate,
	        	"data":data
	        	},
	        dataType:"json",
	        success: function(result){
	        	console.log(result)
	        	allCode = result;
	        },
	        error:function (xml) {
	          console.log(0);
	        }
    });
}
  $("#email").blur(function () {
    var email = $(this).val();
    if (mail.test(email)) {
      $.ajax({
        type: "POST",
        url: "/email",
        data: {
          "email":email
        },
        dataType:"json",
        success: function(result){
          if (result) {
            showMsg('此邮箱可以使用！','top','successful');
          }else{
            showMsg('此邮箱已存在！','top','error');
          }
        },
        error:function (xml) {
          console.log(0);
        }
      });
    }else {
      showMsg('邮箱格式不正确！','top','error');
    }
  })
  $("#name").blur(function () {
    let name = $(this).val();
    console.log(name);
    if (name != ""){
      $.ajax({
        type: "POST",
        url: "/account",
        data: {
          "name":name
        },
        dataType:"json",
        success: function(result){
          if (result) {
            showMsg('此用户名可以使用！','top','successful');
          }else{
            showMsg('此用户名已存在！','top','error');
          }
        },
        error:function (xml) {
          console.log(0);
        }
      });
    }else {
      showMsg('用户名不能为空！','top','error');
    }

  })
  $("#phone").blur(function () {
    var phone = $(this).val();
    if(phoneReg.test(phone)){
      $.ajax({
        type: "POST",
        url: "/phone",
        data: {
          "phone":phone
        },
        dataType:"json",
        success: function(result){
          if (result) {
            showMsg('此手机号可以使用！','top','successful');
          }else{
            showMsg('此手机号已存在！','top','error');
          }
        },
        error:function (xml) {
          console.log(0);
        }
      });
    }else{
      showMsg('请输入正确的手机格式！','top','error');
    }
  })
})